/**
 * TreatmentStemmer.java
 * Copyright (C) 2009 Jose Mauro da Silva Sandy
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

package plagio.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import kea.stopwords.StopwordsEnglish;
import org.tartarus.snowball.ext.englishStemmer;
import plagio.stemmer.StopwordsPortuguese;
import plagio.stemmer.PortugueseStemmer;


/**
 * This class have the objetive of realize one serial of treatments about texts,
 * phrases and words..
 * @author Jose Mauro da Silva Sandy
 * @version Beta
 */
public class TreatmentStemmer {
    /*
     *  @see Constructor default.
     */
    public TreatmentStemmer(){
    }
    /**
     *  @param file: receives the text what will be broken.
     *  @param type: receives the type of text.
     *  @return A vector of string or null if there were any errors.
     */
    public StringBuilder breakText(File file, int type) throws IOException{
        StringBuilder builder = new StringBuilder();
        try {
            // Creating one file in memory to work with words.
            FileInputStream input = new FileInputStream(file);
            InputStreamReader inReader = new InputStreamReader(input);
            BufferedReader reader = new BufferedReader(inReader);
            // Adds all words of text in one variable String.
            // Creates one serial of tokens for break the file.
            char[] tokenPont = {'.', '!', '?', ';'};
            char[] token = {',',':', '(', ')', '/', '{', '}', '[', ']', '|',
                            '\\', '<', '>', '\"'};
            String str = "";
            while(reader.ready()){
                String line = reader.readLine();

                for(int i = 0; i < token.length; i++)
                        line = line.replace(token[i], ' ');

                str += line + " ";
            }

            for(int i = 0; i < tokenPont.length; i++){
                str = str.replace(tokenPont[i], ':');
            }

            String[] phrase = str.split(":");

            str = "";
            for(int j = 0; j < phrase.length; j++){
                String[] strAux = phrase[j].split(" ");
                str = "";
                for(int i = 0; i < strAux.length; i++){
                    if(!strAux[i].toString().trim().equals(""))
                            str += strAux[i] + "_";
                }

                String[] aux = str.split("_");
                if(type == 1){
                    str = removeStopWordPortuguese(aux);

                    if(!str.isEmpty()){
                        builder.append(removeStemmerPortuguese(str)).append('\n');
                    }
                }
            }
        } catch (FileNotFoundException ex) {
            builder = null;
        }
        return builder;
    }

    private String removeStopWordPortuguese(String[] str){
        String result = "";
        try{
            StopwordsPortuguese word = new StopwordsPortuguese();
            for(int i = 0; i < str.length; i++)
                if(!word.isStopword(str[i]))
                    result += str[i] + " ";

        }catch(Exception e){
            result = "";
        }
        return result;
    }

    private String removeStemmerPortuguese(String str){
        String result = "";
        try{
            PortugueseStemmer stem = new PortugueseStemmer();
            result = stem.stemString(str);
        }catch(Exception e){
            result = "";
        }
        return result;
    }

    private String removeStopWordEnglish(String[] str){
        String result = "";
        try{
            StopwordsEnglish word = new StopwordsEnglish();
            for(int i = 0; i < str.length; i++)
                if(!word.isStopword(str[i]))
                    result += str[i] + " ";

        }catch(Exception e){
            result = "";
        }
        return result;
    }

    private String removeStemmerEnglish(String str){
        String result = "";
        try{
            englishStemmer stem = new englishStemmer();
        }catch(Exception e){
            result = "";
        }
        return result;
    }

}
